using System;
using System.Collections.Generic;
using Mall.Api.Entity;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using Mall.Api.ParamModel;
using Newtonsoft.Json;
using Mall.Api.Repository;
using Mall.Api.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authorization;
using Mall.Api.Filters;

namespace Mall.Api.Controllers
{
    /// <summary>
    /// 仓库管理
    /// </summary>


    // [Authorize]

    [ApiController]
    [Route("warehouse")]
    public class WarehouseController : ControllerBase
    {
        private IRepository<Warehouse> _warehouse;

        private readonly IConfiguration _configuration;

        public WarehouseController(IConfiguration configuration, IRepository<Warehouse> warehouse)
        {
            _configuration = configuration;
            _warehouse = warehouse;
        }

        /// <summary>
        /// 获取仓库列表
        /// </summary>
        /// <param name="pager"></param>
        /// <returns></returns>
        [HttpGet]
        public dynamic Get([FromQuery] Pager pager)
        {//get请求默认从uri从获取参数，所以此处应该加上FromQuery的特性才能正确获得参数
            var pageIndex = pager.PageIndex;
            var pageSize = pager.PageSize;

            // 获得所有满足条件的记录
            var warehouses = _warehouse.Table.Where(x => x.IsDeleted == false);
    
            // 对获得的记录分页
           
            if (!string.IsNullOrEmpty(pager.KeyWord))
            {
                warehouses = warehouses.Where(u => u.WarehouseName.Contains(pager.KeyWord));
            }
             var u = warehouses.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            // 返回的数据里，带有数据和分页信息
            var res = new
            {
                Code = 1000,
                Data = new { Data = u, Pager = new { pageIndex, pageSize, rowsTotal = warehouses.Count() } },
                Msg = "获取仓库列表成功^_^"
            };
            return res;
        }







        /// <summary>
        /// 添加仓库
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public string Post(NewWarehouse model)
        {

            var warehouseName = model.WarehouseName;
            var province = model.Province;
            var city = model.City;
            var quantityInstock = model.QuantityInStock;
            var unsuppliedAddress = model.UnsuppliedAddress;
            var w = _warehouse.Table.Where(w => w.WarehouseName == warehouseName).FirstOrDefault();
            if (w != null)
            {

                var tmp = new
                {
                    Code = 1004,
                    Data = "",
                    Msg = "仓库已存在"
                };
                return JsonHelper.Serialize(tmp);
            }

            if (string.IsNullOrEmpty(warehouseName))
            {
                var tmp = new
                {
                    Code = 1004,
                    Data = "",
                    Msg = "仓库名不能为空，请确认后重试"
                };
                return JsonHelper.Serialize(tmp);
            }


            var warehouse = new Warehouse
            {
                WarehouseName = warehouseName,
                Province = province,
                City = city,
                QuantityInStock = quantityInstock,
                UnsuppliedAddress = unsuppliedAddress



            };

            _warehouse.Insert(warehouse);


            var res = new
            {
                Code = 200,
                Data = new
                {
                    Warehouse = warehouse

                },
                Msg = "添加仓库成功"
            };
            return JsonHelper.Serialize(res);
        }






        /// <summary>
        /// 真删除指定仓库
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            var warehouse = _warehouse.GetById(id);
            if (warehouse != null)
            {
                _warehouse.Delete(id);
                return new
                {
                    Code = 200,
                    Msg = string.Format("删除指定id为{0}的仓库成功", id)
                };
            }
            else
            {
                return new
                {
                    Code = 1004,
                    Data = "",
                    Msg = "指定Id的仓库不存在，请确认后重试"
                };
            }

        }







             /// <summary>
        /// 修改仓库信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public dynamic Put(int id, NewWarehouse model)
        {
            var warehouse = _warehouse.GetById(id);


            if (warehouse != null)
            {

                warehouse.WarehouseName =model.WarehouseName;
                warehouse.Province = model.Province;
                warehouse.City = model.City;
                warehouse.QuantityInStock =model.QuantityInStock;
                warehouse.UnsuppliedAddress =model.UnsuppliedAddress;
                warehouse.IsActived =model.IsActived;


                warehouse.UpdatedTime = DateTime.Now;
                _warehouse.Update(warehouse);
                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = warehouse,
                    Msg = string.Format("你修改或删除的仓库的id为：{0}，已经修改成功，请注意查收", id)
                });
            }
            else
            {
                return JsonHelper.Serialize(new
                {
                    Code = 1004,
                    Data = "",
                    Msg = "指定Id的仓库不存在，请确认后重试"
                });
            }
        }








    }
}
